package cn.com.api.service.impl;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.com.api.entity.ApiGoodsTopicLink;
import cn.com.api.entity.ApiSpeicalChang;
import cn.com.api.entity.ApiT_goods_topic;
import cn.com.api.mapper.ApiT_goods_topicMapper;
import cn.com.api.mapper.ApiT_goods_topic_linkMapper;
import cn.com.api.service.ApiT_goods_topicService;
import cn.com.api.service.impl.base.T_goods_topicServiceImplBase;
import cn.com.uthink.util.DateTimeUtil;
import cn.com.uthink.util.StringUtils;
import cn.com.util.PageInfo;
import cn.com.util.Util;

/**
 * 
 **/
@Service
public class ApiT_goods_topicServiceImpl extends T_goods_topicServiceImplBase
		implements ApiT_goods_topicService {
	@Autowired
	private ApiT_goods_topicMapper apiT_goods_topicMapper;
	@Autowired
	private ApiT_goods_topic_linkMapper apiT_goods_topic_linkMapper;

	// 新增对象后返回记录ID
	public int T_goods_topicInsertion(ApiT_goods_topic t_goods_topic) {
		return this.apiT_goods_topicMapper
				.T_goods_topicInsertion(t_goods_topic);
	}

	@Override
	public List<Map<String, Object>> getGoodsTopciInfo() {
		List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
		Map<String, Object> returnMap = null;
		List<Map<String, Object>> topicMap = apiT_goods_topicMapper
				.getAllGoodsTopic();
		if (topicMap != null) {
			for (Map<String, Object> map : topicMap) {
				returnMap = new HashMap<String, Object>();
				returnMap.put("topicId", map.get("topic_id"));
				returnMap.put("title", map.get("title"));
				returnMap.put("startTime", map.get("start_time"));
				returnMap.put("endTime", map.get("end_time"));
				returnMap.put("topicImg", map.get("topic_img"));
				List<ApiGoodsTopicLink> apiGoodsTopicLinks = apiT_goods_topic_linkMapper
						.getApiGoodsTopicLinkByTopicId(map.get("topic_id")
								.toString());
				returnMap.put("topicLinkInfo", apiGoodsTopicLinks);
				list.add(returnMap);
			}
		}
		return list;
	}

	@Override
	public List<Map<String, Object>> getAllTypes() {
		// TODO Auto-generated method stub
		List<Map<String, Object>> list = new ArrayList<>();
		Map<String, Object> returnMap = null;
		List<Map<String, Object>> typelist = apiT_goods_topicMapper
				.getAllTypes();
		int count = 0;
		for (Map<String, Object> map : typelist) {
			returnMap = new HashMap<String, Object>();
			returnMap.put("c_code", map.get("c_code"));
			returnMap.put("c_name", map.get("c_name"));
			returnMap.put("c_ico", map.get("c_ico"));
			// if (count == 0) {
			// LinkedList<Map<String, Object>> topicList =
			// apiT_goods_topicMapper
			// .getTopicByTypeId(typelist.get(0).get("c_code")
			// .toString());
			// for (Map<String, Object> topicMap : topicList) {
			// String discount = apiT_goods_topicMapper
			// .getDiscountByTopicId(topicMap.get("topic_id")
			// .toString());
			// if (!discount.equals("10")) {
			// discount = StringUtils.getFisrtElement(discount);
			// }
			// topicMap.put("discount", discount);
			// }
			// returnMap.put("topicInfo", topicList);
			// }
			list.add(returnMap);
			// count++;
		}
		return list;
	}

	@Override
	public List<Map<String, Object>> getGoodsInfoByTopicId(String topicId,
			String name, int from, int to) {
		// TODO Auto-generated method stub
		return apiT_goods_topicMapper.getGoodInfoByTopicId(topicId, name, from,
				to);
	}

	@Override
	public ApiT_goods_topic getTopicByTopicId(String topicId) {
		// TODO Auto-generated method stub
		return apiT_goods_topicMapper.getTopicByTopicId(topicId);
	}

	@Override
	public void updGoodsTopic(ApiSpeicalChang apiSpeicalChang) {
		apiT_goods_topicMapper.updGoodsTopic(apiSpeicalChang);

	}

	@Override
	public void delGoodsTopic(String topicId) {
		apiT_goods_topicMapper.delGoodsTopic(topicId);
	}

	@Override
	public List<Map<String, Object>> getTopicByCondtion(String typeId,
			String time, PageInfo pageInfo) {
		// TODO Auto-generated method stub

		String currentDate = DateTimeUtil.getTimeByStyle("yyyy-MM-dd");
		String currentHour = DateTimeUtil.getTimeByStyle("HH");
		if (Integer.parseInt(time) < Integer.parseInt(currentHour)) {
			currentDate = DateTimeUtil.getAfterDay();
		}
		List<Map<String, Object>> topicList = apiT_goods_topicMapper
				.getTopicByCondtionListPage(typeId, time, currentDate, pageInfo);
		if (!Util.isNull(topicList)) {
			for (Map<String, Object> map : topicList) {
				if (Integer.parseInt(map.get("end_time").toString()
						.substring(0, 2)) <= Integer.parseInt(DateTimeUtil
						.getTimeByStyle("HH"))) {
					map.put("isOver", 1);
				} else {
					map.put("isOver", 0);
				}

				String discount = apiT_goods_topicMapper
						.getDiscountByTopicId(map.get("topic_id").toString());
				if (!discount.equals("10")) {
					discount = StringUtils.getFisrtElement(discount);
				}
				map.put("discount", discount);
				map.put("TOPIC_ID",
						Integer.parseInt(map.get("topic_id").toString()));
			}
		}
		return topicList;
	}

	@Override
	public TreeMap<String, Object> getSpecailChangTimes() {
		TreeMap<String, Object> timeMap = new TreeMap<>();
		timeMap.put("00:00-01:00", "00:00-01:00");
		timeMap.put("01:00-02:00", "01:00-02:00");
		timeMap.put("02:00-03:00", "02:00-03:00");
		timeMap.put("03:00-04:00", "03:00-04:00");
		timeMap.put("04:00-05:00", "04:00-05:00");
		timeMap.put("05:00-06:00", "05:00-06:00");
		timeMap.put("06:00-07:00", "06:00-07:00");
		timeMap.put("07:00-08:00", "07:00-08:00");
		timeMap.put("08:00-09:00", "08:00-09:00");
		timeMap.put("09:00-10:00", "09:00-10:00");
		timeMap.put("10:00-11:00", "10:00-11:00");
		timeMap.put("11:00-12:00", "11:00-12:00");
		timeMap.put("12:00-13:00", "12:00-13:00");

		timeMap.put("13:00-14:00", "13:00-14:00");
		timeMap.put("14:00-15:00", "14:00-15:00");
		timeMap.put("15:00-16:00", "15:00-16:00");
		timeMap.put("16:00-17:00", "16:00-17:00");
		timeMap.put("17:00-18:00", "17:00-18:00");

		timeMap.put("18:00-19:00", "18:00-19:00");
		timeMap.put("19:00-20:00", "19:00-20:00");
		timeMap.put("20:00-21:00", "20:00-21:00");
		timeMap.put("21:00-22:00", "21:00-22:00");
		timeMap.put("22:00-23:00", "22:00-23:00");
		timeMap.put("23:00-00:00", "23:00-00:00");
		return timeMap;
	}

	public List<Map<String, Object>> getTimeTitle() {
		Calendar now = Calendar.getInstance();
		int hour = now.get(Calendar.HOUR_OF_DAY);
		List<Map<String, Object>> list = new ArrayList<>();
		Map<String, Object> map = null;
		for (int i = 0; i < 24; i++) {
			map = new HashMap<>();
			String num = hour + i + "";
			String endNum = hour + i + 1 + "";

			if (Integer.parseInt(num) < hour) {
				map.put("status", "已结束");
			} else if (Integer.parseInt(num) == hour) {
				map.put("status", "抢购进行中");
			} else {
				map.put("status", "即将开始");
			}

			map.put("date", DateTimeUtil.getTimeByStyle("yyyy-MM-dd"));
			if (Integer.parseInt(num) >= 24) {
				map.put("status", "即将开始");
				num = Integer.parseInt(num) - 24 + "";
				map.put("date", DateTimeUtil.getAfterDay());
			}

			if (Integer.parseInt(endNum) >= 24) {
				endNum = Integer.parseInt(endNum) - 24 + "";
			}

			if (num.length() < 2) {
				num = "0" + num;
			}
			if (endNum.length() < 2) {
				endNum = "0" + endNum;
			}

			String timeStr = num + ":00-" + endNum + ":00";
			map.put("time", timeStr);

			list.add(map);
		}
		return list;
	}

}
